package archforce.atp.x.ui.frame.component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author wx
 * @version 1.0
 * @date 2022/4/8 16:40
 */
public class RowPanel<T extends AbstractRowItemComponent<D>, D> extends JPanel {

    private Integer id;
    private final JRadioButton selectBtn = new JRadioButton();
    private T rowComponent;
    private final JButton delBtn = new JButton("删除");
    private ButtonGroup group;
    private FacedComponent<T,D> relatedPanel;
    private DelAction delAction;

    public RowPanel(Integer id, ButtonGroup group, Class<? extends AbstractRowItemComponent<D>> rowItemType) {
        super();
        init(id, group, null, rowItemType);
    }

    public RowPanel(Integer id, ButtonGroup group, D bindData, Class<? extends AbstractRowItemComponent<D>> rowItemType) {
        super();
        init(id, group, bindData, rowItemType);
    }

    protected void init(Integer id, ButtonGroup group, D bindData, Class<? extends AbstractRowItemComponent<D>> rowItemType){
        this.id = id;
        this.group = group;
        this.delAction = new DelAction(this.delBtn, this.relatedPanel);
        this.delBtn.setName(String.valueOf(id));
        this.delBtn.addMouseListener(delAction);
        this.selectBtn.setName(String.valueOf(id));
        this.selectBtn.setActionCommand(String.valueOf(id));
        this.group.add(this.selectBtn);
        try {
            Optional<Constructor<?>> first = Stream.of(rowItemType.getConstructors())
                    .filter(constructor -> constructor.getParameterCount() <= 0)
                    .findFirst();
            rowComponent = (T) first.get().newInstance();
            rowComponent.addInputMethodListener(new InputMethodListener() {
                @Override
                public void inputMethodTextChanged(InputMethodEvent event) {

                }

                @Override
                public void caretPositionChanged(InputMethodEvent event) {

                }
            });
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        this.rowComponent.setBindData(bindData);
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        flowLayout.setHgap(0);
        flowLayout.setVgap(0);
        setLayout(flowLayout);

        add(this.selectBtn);
        add(rowComponent);
        add(delBtn);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        this.delBtn.setName(String.valueOf(id));
        this.selectBtn.setName(String.valueOf(id));
        this.selectBtn.setActionCommand(String.valueOf(id));
    }

    public D getBindData() {
        return this.rowComponent.getBindData();
    }

    public void setBindData(D bindData) {
        this.rowComponent.setBindData(bindData);
    }

    public FacedComponent<T,D> getRelatedPanel() {
        return relatedPanel;
    }

    public void setRelatedPanel(FacedComponent<T,D> relatedPanel) {
        this.relatedPanel = relatedPanel;
        this.delAction.setRelatedPanel(relatedPanel);
    }

    public void addActionListener(ActionListener listener){
        this.selectBtn.addActionListener(listener);
    }

    public ActionListener[] getActionListeners(){
        ActionListener[] actionListeners = this.selectBtn.getActionListeners();
        return actionListeners;
    }

    public ActionListener getActionListener(){
        ActionListener[] actionListeners = this.selectBtn.getActionListeners();
        return actionListeners == null|| actionListeners.length <=0? null:actionListeners[0];
    }

    class DelAction implements MouseListener{
        private JButton delBtn;
        private FacedComponent<T,D> relatedPanel;
        public DelAction(JButton delBtn, FacedComponent<T,D> releatedPanel) {
            this.delBtn = delBtn;
            this.relatedPanel = releatedPanel;
        }

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            String id = delBtn.getName();
            System.out.println("delete index:" + id);
            if(relatedPanel != null){
                relatedPanel.removeRow(Integer.valueOf(id));
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }

        public JButton getDelBtn() {
            return delBtn;
        }

        public void setDelBtn(JButton delBtn) {
            this.delBtn = delBtn;
        }

        public FacedComponent<T,D> getRelatedPanel() {
            return relatedPanel;
        }

        public void setRelatedPanel(FacedComponent<T,D> relatedPanel) {
            this.relatedPanel = relatedPanel;
        }
    }
}
